<?
/**
 * Manage Users Records
 * 
 * @name manage users records
 * @version		0.3
 * @uses		Wise_Database_Mysql
 * @package		nats-space
 * @see			http://code.google.com/p/nats-space/
 * @copyright	2008-2009
 * @author		Joseph C. <joseph.cheys@gmail.com>
 * 
 * @todo avoid dependency for Wise_Database_Mysql and use Zend_Db instead
 */
class Nats3space_Users_Management
{
	/**
	 * Used to enable user
	 */
	const STATUS_ENABLED = 1;
	
	/**
	 * Used to disable user
	 */
	const STATUS_DISABLED = 0;
	
	/**
	 * Error messages
	 * 
	 * @var string
	 */
	public $error_msg = '';

	/**
	 * Member primary site
	 * 
	 * @var string 
	 */
	public $site_id;
	
	/**
	 * Wise_Database_Mysql class instance
	 * 
	 * @var Wise_Database_Mysql
	 */
	private $db;

	/**
	 * Constructor
	 */
	public function __construct($site_id)
	{
		$this->db = Wise_Database_Mysql::getInstance();
		$this->site_id = intval($site_id);
	}

	/**
	 * Inserts new member
	 * 
	 * @param string @username
	 * @param string @password
	 * @patam boolean $is_trial set to true if user membership is trial 
	 * @return int last insert ID
	 */
	private function register($username, $password, $is_trial = false)
	{
		$sql = "
		INSERT INTO 
			members 
		SET
			  username = '".$this->db->escape($username)."'
			, password = '".$this->db->escape($password)."'
			, status = '".self::STATUS_ENABLED."'
			, siteid = ".$this->site_id."
			".($is_trial ? ', trial = 1 ' : '')."			
			, created = NOW()
		";
		$res = $this->db->runQuery($sql);
		
		if (!empty($res))
			return $this->db->getLastInsertID();
		
		// User wasn't registerred
		$this->error_msg = 'User '.$username.' wasnt registerred: '.$this->db->error_msg;
		return false;
	}
	
	/**
	 * Enable user
	 * 
	 * @param string $username
	 * @param string $password
	 * @patam boolean $is_trial set to true if user membership is trial 
	 * @return boolean true if member was enabled or false otherwise
	 */
	public function enable($username, $password, $is_trial = false)
	{
		/**
		 * Check username for existance
		 * If user still not registerred 
		 * then insert new user record into database
		 */
		if (!$this->check($username, false)) 
		{
			$res = $this->register($username, $password, $is_trial);
			
			if (!$res)
				return false;

		/**
		 * else update user status
		 */
		} else {
			
			$sql = "
			UPDATE
				members
			SET
				password = '".$this->db->escape($password)."'
				, status = '".self::STATUS_ENABLED."'
				".($is_trial ? ', trial = 1 ' : '')."
			WHERE
				username = '".$this->db->escape($username)."'
			LIMIT 1
			";
	
			$res = $this->db->runQuery($sql);

		}

		if (!empty($res))
			return true;

		// User wasn't enabled
		$this->error_msg = 'User '.$username.' wasnt enabled: '.$this->db->error_msg;
		return false;
	}
	
	/**
	 * Disable user
	 * 
	 * @param $username string
	 * @return boolean true if member was disabled or false otherwise
	 */
	public function disable($username)
	{
		$sql = "
		UPDATE
			members
		SET
			status = '".self::STATUS_DISABLED."'
		WHERE
			username = '".$this->db->escape($username)."'";

		$res = $this->db->runQuery($sql);
		
		if ($this->db->getAffectedRows() > 0)
			return true;
		
		// User wasn't enabled
		$this->error_msg = 'User '.$username.' wasnt disabled: '.$this->db->error_msg;
		return false;		
	}
	
	/**
	 * Checks user for existance
	 * 
	 * @param string user name to check
	 * @param boolean set to false if you want to ignore member status
	 * @return boolean true if user exists or false otherwise
	 */
	public function check($username, $including_status = true)
	{
		$sql = "
		SELECT
			username
		FROM
			members
		WHERE
			username = '".$this->db->escape($username)."'
			".(($including_status) ? " AND status = '".self::STATUS_ENABLED."' " : '');

		$res = $this->db->getCell($sql);
		
		if (!empty($res))
			return true;
		
		return false; 
	}

}